﻿using BotDetector.Core.NaiveBayesModule;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BotDetector.Core.Utilities
{
    public class FileOperator
    {
        public List<string> ReadFileSet(string folderPath)
        {
            List<string> fileList = new List<string>();

            foreach (string file in Directory.EnumerateFiles(folderPath))
            {
                try
                {
                    string content = File.ReadAllText(file);
                    fileList.Add(content);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                }
            }
            return fileList;
        }

        public List<Tuple<string, string>> ReadFileSetAndNames(string folderPath)
        {
            List<Tuple<string, string>> fileList = new List<Tuple<string, string>>();

            foreach (string file in Directory.EnumerateFiles(folderPath))
            {
                try
                {
                    string content = File.ReadAllText(file);
                    string name = Path.GetFileName(file);
                    fileList.Add(new Tuple<string, string>(name, content));
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                }
            }
            return fileList;
        }

        public void WriteFileSet(List<Tuple<string, string>> fileNameAndContentList)
        {
            foreach(Tuple<string, string> x in fileNameAndContentList)
            {
                StringToFile(x.Item1, x.Item2);
            }
        }

        public void StringToFile(string path, string content)
        {
            try
            {
                File.WriteAllText(path, content);
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.StackTrace);
            }
        }

        public void StringListToFile(string path, List<string> strings)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
            {
                foreach(string s in strings)
                {
                    file.WriteLine(s);
                }
            }
        }

        public void DeleteFiles(string folderPath)
        {
            foreach (string file in Directory.EnumerateFiles(folderPath))
            {
                File.Delete(file);
            }
        }

        public void CopyFiles(string sourceFolder, string destinationFolder)
        {
            if (System.IO.Directory.Exists(sourceFolder))
            {
                string[] files = System.IO.Directory.GetFiles(sourceFolder);

                foreach (string s in files)
                {
                    string fileName = Path.GetFileName(s);
                    string destFile = Path.Combine(destinationFolder, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
        }
        
        public bool IsDirectoryEmpty(string filePath)
        {
            return !Directory.EnumerateFileSystemEntries(filePath).Any();
        }
    }
}
